package com.fifth.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.commodity.vo.BrandStateVo;
import com.fifth.commodity.vo.BrandVo;
import com.fifth.commodity.vo.StateVo;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Brand;
import com.fifth.commodity.mapper.BrandMapper;
import com.fifth.commodity.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /*
    *
    * 查询所有
    */
    @Override
    public CommonResult selectAll(Brand brand) {

        List<Brand> deposits = brandMapper.selectAll(brand);

            if (deposits!=null){
                return new CommonResult(2000,"查询成功",deposits);
            }else {
                return new CommonResult(5000,"查询失败",null);


        }


    }
    /*
    *
    *
    * 添加
    * */
    @Override
    public CommonResult insertBrand(Brand brand) {
        int e = brandMapper.insert(brand);
        if (e==1){
            return new CommonResult(2000,"添加成功",e);
        }else {
            return new CommonResult(5000,"添加失败",null);
        }
    }
    /*
    *
    *
    * 修改
    * */
    @Override
    public CommonResult updateById(Brand brand) {

        int i = brandMapper.updateById(brand);


        if (i==1){
            return new CommonResult(2000,"修改成功",i);
        }else {
            return new CommonResult(5000,"修改失败",null);
        }
    }

    /**
     * 根据条件所有实体类
     * @param pageCurrent
     * @param pageSize
     * @param brandVo
     * @return
     */
    @Override
    public CommonResult findBrandsWithCondiction(Integer pageCurrent, Integer pageSize, BrandVo brandVo) {
        IPage<Brand> page = new Page<>(pageCurrent,pageSize);
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        if (brandVo.getBrandid()!=null) {
            queryWrapper.eq("g_brand.id", brandVo.getBrandid());
        }
        if (brandVo.getStatus()!=null){
            queryWrapper.eq("g_brand.status",brandVo.getStatus());
        }
        if (brandVo.getSortid()!=null){
            queryWrapper.eq("g_brand.sortid",brandVo.getSortid());
        }
        if (brandVo.getStatus()==null && brandVo.getBrandid()==null && brandVo.getSortid()==null){
            queryWrapper = null;
        }
        IPage<Brand> brandIPage = brandMapper.findBrandsWithCondiction(page, queryWrapper);
        return new CommonResult(2000,"查询成功",brandIPage);
    }

    /**
     * 查询所有品牌
     * @return
     */
    @Override
    public CommonResult findAllBrands() {
        List<Brand> brands = brandMapper.selectList(null);
        return new CommonResult(2000,"查询成功",brands);
    }

    @Override
    public CommonResult deleteBrand(Integer id) {
        int i = brandMapper.deleteById(id);
        if (i==1){
            return new CommonResult(2000,"删除成功",null);
        }else {
            return new CommonResult(5000,"删除失败",null);
        }
    }

    /**
     * 修改审核状态
     * @param brandStateVo
     * @return
     */
    @Override
    public CommonResult updateState(BrandStateVo brandStateVo) {
        Brand brand = new Brand();
        brand.setId(brandStateVo.getId());
        brand.setCheckContent(brandStateVo.getCheckContent());
        brand.setStatus(brandStateVo.getState());
        int i = brandMapper.updateById(brand);
        if (i==1){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    /**
     * 页面加载查询所有品牌
     * @return
     */
    @Override
    public CommonResult selectAllBrand() {
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        List<Brand> brands = brandMapper.selectList(queryWrapper);
        return new CommonResult(2000,"查询成功",brands);
    }


}
